<?php

class Rzd_Controller_Plugin_Statistics extends Zend_Controller_Plugin_Abstract
{
   public function postDispatch(Zend_Controller_Request_Abstract $request)
   {
        if(!Zend_Auth::getInstance()->hasIdentity()) {
            return false;
        }

        $urlParams = $request->getParams();
        foreach($urlParams as $param => $value) {
            if(is_array($value) || is_object($value)) {
                unset($urlParams[$param]);
            }
        }

        $this->checkAndSave(implode('/', $urlParams));
  }

  public function checkAndSave($page)
  {
        $statistics = Zend_Session::namespaceGet('Statistics');

        if(!$statistics) {
           $statistics = new Zend_Session_Namespace('Statistics');
           $statistics->last_page = $page;

        }

        $values = array(
            'user_id' => Zend_Auth::getInstance()->getStorage()->read()->id,
            'page' => $page
        );

        if($statistics['last_page'] != $page) {
            $statisticsDbMapper = new Default_Model_DbMapper_Statistics();
            $statisticsDbMapper->insert($values);
        }
  }
}
